package com.otupia.blog.facade;

import java.util.List;

import com.cvlep.appbase.locator.Locator;
import com.otupia.blog.locator.BlogSpringLocator;
import com.otupia.blog.model.Blog;
import com.otupia.blog.service.BlogSearchService;
import com.otupia.blog.service.BlogWriteService;

public class BlogServiceBean implements BlogService
{

	private Locator locator = new BlogSpringLocator();
	private BlogSearchService blogSearcher = (BlogSearchService)this.locator.getBean("blogSearchService");
	private BlogWriteService blogWriter = (BlogWriteService)this.locator.getBean("blogWriteService");

	@Override
	public void writeBlog(Blog blog)
	{
		this.blogWriter.postBlog(blog);
	}

	@Override
	public void updateBlog(Blog blog)
	{
		this.blogWriter.updateBlog(blog);
	}

	@Override
	public void deleteBlog(Blog blog)
	{
		this.blogWriter.deleteBlog(blog.getId());
	}

	@Override
	public Blog queryBlogById(String id)
	{
		long idAsLong = -1l;

		try
		{
			idAsLong = Long.parseLong(id);
		} catch (NumberFormatException ne)
		{
			return null;
		}

		return this.blogSearcher.getBlogById(idAsLong);
	}

	@Override
	public List<Blog> queryBlogByWriterId(String id)
	{
		long idAsLong = -1l;

		try
		{
			idAsLong = Long.parseLong(id);
		} catch (NumberFormatException ne)
		{
			return null;
		}

		return this.blogSearcher.getBlogsByWriterId(idAsLong);
	}

}
